<template>
    <view class="notice-container">
      <!-- 顶部搜索栏 -->
      <uni-search-bar @confirm="searchNotice" v-model="queryParams.noticeTitle" placeholder="请输入公告标题"></uni-search-bar>
  
      <!-- 公告列表 -->
      <scroll-view scroll-y :scroll-top="scrollTop" @scroll="handleScroll" style="height: 100vh;">
        <uni-list v-if="noticeList.length > 0">
            <uni-list-item 
                v-for="notice in noticeList" 
                :key="notice.noticeId"
                :title="notice.noticeTitle"
                clickable
                :rightText="formatNote(notice)"
                @click="goToDetail(notice.noticeId)" 
                />
        </uni-list>
        <uni-load-more :status="loadingStatus" v-if="noticeList.length > 0" />
      </scroll-view>
  
      <!-- 空数据提示 -->
      <view v-if="noticeList.length === 0" class="empty-data">暂无通知公告</view>
    </view>
  </template>
  
  <script>
  import { listNotice } from '@/api/system/notice';

  export default {
    data() {
      return {
        noticeList: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          noticeTitle: ''
        },
        loadingStatus: 'more',
        total: 0,
        scrollTop: 0
      };
    },
    onReady() {
      this.getList();
    },
    methods: {
      // 格式化 note 显示内容
        formatNote(notice) {
            const typeLabel = notice.noticeType === '1' ? '通知' : '公告';
            const statusLabel = notice.status === '0' ? '正常' : '关闭';
            return `${typeLabel} | ${statusLabel}`;
        },
      // 获取公告列表
      async getList() {
        listNotice(this.queryParams).then(res => {
          this.noticeList = res.rows;
          this.total = res.total; 
          this.loadingStatus = this.noticeList.length >= this.total ? 'noMore' : 'more';
        })
      },
  
      // 加载更多
      handleScroll() {
        if (this.loadingStatus !== 'more') return;
        this.queryParams.pageNum += 1;
        this.getList();
      },
  
      // 跳转到详情
      goToDetail(noticeId) {
        this.$tab.navigateTo('/pages/system/notice-detail?noticeId=' + noticeId);
      },
  
      // 根据 noticeType 显示中文标签
      getNoticeTypeLabel(type) {
        return type === '1' ? '通知' : '公告';
      },
  
      // 搜索
      searchNotice() {
        this.queryParams.pageNum = 1;
        this.noticeList = [];
        this.getList();
      }
    }
  };
  </script>
  
  <style scoped>
  .notice-container {
    padding: 20rpx;
  }
  .empty-data {
    text-align: center;
    margin-top: 200rpx;
    color: #999;
  }
  </style>